﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using CoreGallery.Addins.Contracts;

namespace CoreGallery.Storage.fs
{
    /// <summary>
    /// Works with photos directly from the filesystem
    /// </summary>
    [CoreAddin("Basic filesystem storage engine", "{BCE17BE1-E168-4da9-841B-71342C9774B1}",
        Description = "Uses a local or network hard drive as direct storage for photos.")]
    public class FileSystemStorageProvider : IStorageProviderBase
    {
        #region IStorageProviderBase Members

        public IList<BaseGallery> Galleries
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public Guid UniqueKey
        {
            get { return new Guid("{BCE17BE1-E168-4da9-841B-71342C9774B1}"); }
        }

        public string Name
        {
            get { return "Basic filesystem storage engine"; }
        }

        public string Creator
        {
            get { throw new NotImplementedException(); }
        }

        public string Description
        {
            get { return "Uses a local or network hard drive as direct storage for photos."; }
        }

        public string AboutText
        {
            get { return "Core storage engine for filesystem"; }
        }

        public Version Version
        {
            get { return new Version(1, 0); }
        }

        public BaseGallery CreateGallery()
        {
            throw new NotImplementedException();
        }

        public BaseAlbum CreateAlbum(BaseGallery g)
        {
            throw new NotImplementedException();
        }

        // TODO: Does it even make sense to create a photo this way?  Use cases would
        // include importing and automated processes (i.e. feeds) from within the provider.
        public BasePhoto CreatePhoto(BaseAlbum a)
        {
            throw new NotImplementedException();
        }

        // Some providers would include user ID of owner, folder/tags from site, etc.
        private BasePhoto CreatePhoto(string filename)
        {
            BasePhoto p = new BasePhoto();

            BitmapDecoder decoder = BitmapDecoder.Create(new Uri(filename), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
            p.Width = (long)decoder.Frames[0].Width;
            p.Height = (long)decoder.Frames[0].Height;
            p.Metadata.Add(new MetadataElement("Title", decoder.Metadata.Title));
            p.Metadata.Add(new MetadataElement("Author", string.Join(",", decoder.Metadata.Author.ToArray())));
            p.Metadata.Add(new MetadataElement("Comment", decoder.Metadata.Comment));
            p.Metadata.Add(new MetadataElement("DateTaken", decoder.Metadata.DateTaken));
            p.Metadata.Add(new MetadataElement("Keywords", string.Join(",", decoder.Metadata.Keywords.ToArray())));
            p.Metadata.Add(new MetadataElement("Location", decoder.Metadata.Location));
            p.Metadata.Add(new MetadataElement("Rating", decoder.Metadata.Rating + " stars"));

            return p;
        }

        public void RemoveGallery(BaseGallery g)
        {
            throw new NotImplementedException();
        }

        public void OnInit()
        {
            //Application.Debug("FileSystemStorageProvider init");
        }

        public void OnLoad()
        {
            //Application.Debug("FileSystemStorageProvider load");
        }

        public void OnUnload()
        {
            //Application.Debug("FileSystemStorageProvider unload");
        }

        #endregion
    }
}
